//package org.finesys.common.security.core.jackson;
//
//
//import com.fasterxml.jackson.core.JsonParseException;
//import com.fasterxml.jackson.core.JsonParser;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.*;
//import com.fasterxml.jackson.databind.node.MissingNode;
//import org.finesys.common.security.core.util.OAuth2JacksonUtils;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.oauth2.core.AuthorizationGrantType;
//import org.springframework.security.oauth2.core.OAuth2AccessToken;
//
//import java.io.IOException;
//import java.time.Instant;
//import java.util.List;
//import java.util.Map;
//import java.util.Set;
//
//
//class OAuth2AccessTokenDeserializer extends JsonDeserializer<OAuth2AccessToken> {
//    private static final TypeReference<List<GrantedAuthority>> GRANTED_AUTHORITY_LIST = new TypeReference<List<GrantedAuthority>>() {
//    };
//    private static final TypeReference<Object> OBJECT = new TypeReference<Object>() {
//    };
//
//    OAuth2AccessTokenDeserializer() {
//    }
//
//    public OAuth2AccessToken deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
//
//        ObjectMapper mapper = (ObjectMapper) jp.getCodec();
//        JsonNode jsonNode = (JsonNode) mapper.readTree(jp);
//        Boolean authenticated = this.readJsonNode(jsonNode, "authenticated").asBoolean();
//        JsonNode principalNode = this.readJsonNode(jsonNode, "principal");
//        Object principal = this.getPrincipal(mapper, principalNode);
//        JsonNode credentialsNode = this.readJsonNode(jsonNode, "credentials");
//        Object credentials = this.getCredentials(credentialsNode);
//        List<GrantedAuthority> authorities = (List) mapper.readValue(this.readJsonNode(jsonNode, "authorities").traverse(mapper), GRANTED_AUTHORITY_LIST);
//
//        String grantType = this.readJsonNode(jsonNode, "authorizationGrantType").asText("");
//        AuthorizationGrantType authorizationGrantType = new AuthorizationGrantType(grantType);
//        JsonNode authenticationNode = this.readJsonNode(jsonNode, "authentication");
//        Authentication authentication = OAuth2JacksonUtils.jsonNodeToAuthentication(authenticationNode);
//        JsonNode scopesNode = this.readJsonNode(jsonNode, "scopes");
//        Set<String> scopes = OAuth2JacksonUtils.jsonNodeToSet(scopesNode);
//        JsonNode additionalParamsNode = this.readJsonNode(jsonNode, "additionalParams");
//        Map<String, Object> additionalParams = additionalParamsNode.deepCopy();
//
//
//        OAuth2AccessToken.TokenType tokenType;
//        String tokenValue;
//        Instant issuedAt;
//        Instant expiresAt;
//        Set<String> scopes;
//
//
//        OAuth2AccessToken token = new OAuth2AccessToken(tokenType, tokenValue, issuedAt, expiresAt, scopes);
//        return token;
//    }
//
//    private Object getCredentials(JsonNode credentialsNode) {
//        return !credentialsNode.isNull() && !credentialsNode.isMissingNode() ? credentialsNode.asText() : null;
//    }
//
//    private Object getPrincipal(ObjectMapper mapper, JsonNode principalNode) throws IOException, JsonParseException, JsonMappingException {
//        return principalNode.isObject() ? mapper.readValue(principalNode.traverse(mapper), Object.class) : principalNode.asText();
//    }
//
//    private JsonNode readJsonNode(JsonNode jsonNode, String field) {
//        return (JsonNode) (jsonNode.has(field) ? jsonNode.get(field) : MissingNode.getInstance());
//    }
//}